<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of Controller
 *
 * @author Lille
 */
class Koncept_Rest_Resource_Controller extends Zend_Controller_Action {
    //put your code here

    protected $allow = array('GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS');

    public function init()
    {
    }

    protected function _notAllowed()
    {
        // Disable auto-rendering
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        // Set Allow header and 405 Code
        $this->getResponse()->setHeader('Allow', $this->allow);
        $this->getResponse()->setHttpResponseCode(405);
        echo "<h1>405 Method " . strtoupper($this->getRequest()->getMethod()) . " not allowed</h1>\n<p>Methods allowed are: " . implode(', ', $this->allow) . ".</p>";
    }


    protected function _notFound($message = 'Page not found')
    {
        // Disable auto-rendering
        $this->_helper->viewRenderer->setNoRender();
        // Set Allow header and 405 Code
        $this->getResponse()->setHttpResponseCode(404);
        echo "<h1>$message</h1>";
    }

    public function __call($method, $args)
    {
        // Set action to <http_method>Action
        $action = strtolower($this->getRequest()->getParam('action')) . 'Action';
        if (method_exists($this, $action)) {
            return call_user_func_array(array($this, $action), $args);
        }
        if(strtolower($this->getRequest()->getMethod()) == strtolower($this->getRequest()->getParam('action'))) {
            $this->_notAllowed();
        } else {
            $this->_notFound();
        }
    }

    public function optionsAction()
    {
        // Set Allow header and 200 Code
        $this->getResponse()->setHeader('Allow', $this->allow);
        $this->getResponse()->setHttpResponseCode(200);
        echo "<h1>200 Ok</h1>\n<p>Methods allowed are: " . implode(', ', $this->allow) . ".</p>";
    }

    /**
     * Dispatch the requested action
     *
     * @param string $action Method name of action
     * @return void
     */
    public function dispatch($action)
    {
        // Notify helpers of action preDispatch state
        $this->_helper->notifyPreDispatch();

        $this->preDispatch();
        if ($this->getRequest()->isDispatched()) {
            if (null === $this->_classMethods) {
                $this->_classMethods = get_class_methods($this);
            }
            $actionParams = $this->getRequest()->getParams();
            // preDispatch() didn't change the action, so we can continue
            if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
                if ($this->getInvokeArg('useCaseSensitiveActions')) {
                    trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
                }
            }

            // If method does not exist, call index or get
            $actionParams = $this->_getParam('actionParams');
            if(!in_array($action, $this->_classMethods)) {
                if(empty($actionParams)) {
                    $action = 'index';
                } else {
                    $action = 'get';
                }
            }
            $this->__call($action, $actionParams);
            $this->postDispatch();
        }

        // whats actually important here is that this action controller is
        // shutting down, regardless of dispatching; notify the helpers of this
        // state
        $this->_helper->notifyPostDispatch();
    }
}
?>
